import { createRouter, createWebHashHistory } from "vue-router";

//1.定义用于路由访问的组件
import HomePage from "../views/HomePage.vue";
import AboutPage from "../views/AboutPage.vue";
import UserPage from "../views/UserPage.vue";

//2.定义路由表
const routes = [
    {
        path: "/",
        component: HomePage,
        meta: { msg: "路由元数据" }
    },
    {
        path: "/about",
        component: AboutPage
    },
    {
        path: "/user/:key",
        component: UserPage
    }
];

// 3.创建路由
const router = createRouter({
    history: createWebHashHistory(),
    routes
});

// 4.前置路由守卫
router.beforeEach((to, from) => {
    console.log(`from:${from.fullPath} --> to:${to.fullPath}`);
    if (to.meta.msg) {
        console.log(to);
        console.log(to.meta.msg);
    }
});

export default router;
